//
//  SearchViewController.m
//  民族文化宫
//
//  Created by kobehjk on 14-7-4.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "SearchViewController.h"
#import "MZSection.h"
#import "NSObject+Value.h"
#import "MZNature.h"
#import "MZMetaDataTool.h"
#import "SearchresultTableViewController.h"

@interface SearchViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

{
    UISearchBar *_searchBar;
    UITableView *_tableView;
    NSMutableArray *_minzuSections; // 所有的民族组信息
    UIView *_cover; // 遮盖
    SearchresultTableViewController *_searchResult;

}
@property (strong,nonatomic)NSArray *nature;

@property (strong,nonatomic)NSArray *nature2;
@end

@implementation SearchViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"搜索";
    // 1.添加搜索框
    [self addSearchBar];
    
    // 2.添加tableView
    [self addTableView];
    
    // 3.加载数据
    [self loadCitiesData];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    

}

- (void)addSearchBar
{
    UISearchBar *search = [[UISearchBar alloc] init];
    search.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    search.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
    search.delegate = self;
    search.placeholder = @"请输入民族名称，例“汉族”或者“hanzu”";
    //    search.tintColor 渐变色
    //    search.barStyle 样式
    [self.view addSubview:search];
    _searchBar = search;
}

- (void)addTableView
{
    UITableView *tableView = [[UITableView alloc] init];
    CGFloat h = self.view.frame.size.height - 40;
    tableView.frame = CGRectMake(0, 40, self.view.frame.size.width, h);
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:tableView];
    _tableView = tableView;
}

- (void)loadCitiesData
{
    _nature = [NSMutableArray array];

    _nature = [NSArray arrayWithObjects:@"阿昌族",@"布依族",@"布朗族",@"白族",@"保安族",@"朝鲜族",@"傣族",@"德昂族",@"东乡族",@"独龙族",@"侗族",@"达斡尔族",@"鄂伦春族",@"鄂温克族",@"俄罗斯族",@"仡佬族",@"高山族",@"汉族",@"赫哲族",@"哈尼族",@"回族",@"哈萨克族",@"京族",@"景颇族",@"基诺族",@"柯尔克孜族",@"黎族",@"拉祜族",@"珞巴族",@"傈僳族",@"仫佬族",@"满族",@"蒙古族",@"毛南族",@"苗族",@"门巴族",@"怒族",@"纳西族",@"普米族",@"羌族",@"撒拉族",@"畲族",@"水族",@"塔塔尔族",@"土族",@"土家族",@"塔吉克族",@"佤族",@"维吾尔族",@"乌兹别克族",@"锡伯族",@"裕固族",@"瑶族",@"彝族",@"藏族",@"壮族", nil];
    _nature1 = @[@{@"name" : @"A", @"natures" : @[@"阿昌族"]},@{@"name" : @"B", @"natures" :  @[@"布依族",@"布朗族",@"保安族"]},
                 @{@"name" : @"C", @"natures" : @[@"朝鲜族"]},@{@"name" : @"D", @"natures" : @[@"傣族",@"德昂族",@"东乡族",@"独龙族",@"侗族",@"达斡尔族"]},
                 @{@"name" : @"E", @"natures" : @[@"鄂伦春族",@"鄂温克族",@"俄罗斯族"]},
                 //  @{@"index" : @"F", @"key" : @[]},
                 @{@"name" : @"G", @"natures" : @[@"仡佬族",@"高山族"]},@{@"name" : @"H", @"natures" : @[@"汉族",@"赫哲族",@"哈尼族",@"回族",@"哈萨克族"]},
                 //  @{@"index" : @"I", @"key" : @[]},
                 @{@"name" : @"J", @"natures" : @[@"京族",@"景颇族",@"基诺族"]},
                 @{@"name" : @"K", @"natures" : @[@"柯尔克孜族"]},@{@"name" : @"L", @"natures" : @[@"黎族",@"拉祜族",@"珞巴族",@"傈僳族"]},
                 @{@"name" : @"M", @"natures" : @[@"仫佬族",@"满族",@"蒙古族",@"毛南族",@"苗族",@"门巴族"]},@{@"name" : @"N", @"natures" : @[@"怒族",@"纳西族"]},
                 //  @{@"index" : @"O", @"key" : @[]},
                 @{@"name" : @"P", @"natures" : @[@"普米族"]},
                 @{@"name" : @"Q", @"natures" : @[@"羌族"]},
                 //  @{@"index" : @"R", @"key" : @[]},
                 @{@"name" : @"S", @"natures" : @[@"撒拉族",@"畲族",@"水族"]},@{@"name" : @"T", @"natures" : @[@"塔塔尔族",@"土族",@"土家族",@"塔吉克族"]},
                 //  @{@"name" : @"U", @"key" : @[]},@{@"index" : @"V", @"key" : @[]},
                 @{@"name" : @"W", @"natures" : @[@"塔吉克族",@"佤族",@"维吾尔族",@"乌兹别克族"]},@{@"name" : @"X", @"natures" : @[@"锡伯族"]},
                 @{@"name" : @"Y", @"natures" : @[@"裕固族",@"瑶族",@"彝族"]},@{@"name" : @"Z", @"natures" : @[@"藏族",@"壮族"]},
                 ];
    //    NSLog(@"%@",_nature1  );
//    NSLog(@"%@",_nature  );

    _minzuSections = [NSMutableArray array];
//    for (NSDictionary *dict in _nature1  ) {
//        MZSection *section = [[MZSection alloc]init];
//        [section setValues:dict];
//        [_minzuSections addObject:section];
//    }
    
    NSArray *sections = [MZMetaDataTool sharedMZMetaDataTool].totalCitySections;
    [_minzuSections addObjectsFromArray:sections];
    NSLog(@"_minzuSections%@",sections);


}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0) {
        // 隐藏搜索界面
        [_searchResult.view removeFromSuperview];
    } else {
        // 显示搜索界面
        if (_searchResult == nil) {
            _searchResult = [[SearchresultTableViewController alloc] init];
            _searchResult.view.frame = _cover.frame;
            _searchResult.view.autoresizingMask = _cover.autoresizingMask;
            [self addChildViewController:_searchResult];
        }
        _searchResult.searchText = searchText;
        [self.view addSubview:_searchResult.view];
    }
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    // 1.显示取消按钮
//    [searchBar setShowsCancelButton:YES animated:YES];
    
    // 2.显示遮盖（蒙板）
    if (_cover == nil) {
        _cover = [[UIView alloc] init];
        _cover.backgroundColor = [UIColor blackColor];
        _cover.frame = _tableView.frame;
        _cover.autoresizingMask = _tableView.autoresizingMask;
        [_cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)]];
    }
    [self.view addSubview:_cover];
    _cover.alpha = 0.0;
    [UIView animateWithDuration:0.3 animations:^{
        _cover.alpha = 0.7;
    }];
}


- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [self coverClick];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self coverClick];
}

- (void)coverClick
{
    // 1.移除遮盖
    [UIView animateWithDuration:0.3 animations:^{
        _cover.alpha = 0.0;
    } completion:^(BOOL finished) {
        [_cover removeFromSuperview];
    }];
    
    // 2.隐藏取消按钮
    [_searchBar setShowsCancelButton:NO animated:YES];
    
    // 3.退出键盘
    [_searchBar resignFirstResponder];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _minzuSections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    MZSection *s = _minzuSections[section];
    return s.natures.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    
    MZSection *s = _minzuSections[indexPath.section];
    cell.textLabel.text = s.natures[indexPath.row];

    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    MZSection *s = _minzuSections[section];
    return s.name;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    // 会取出_citySections中所有元素name属性的值，并且放到数组中返回
    return [_minzuSections valueForKeyPath:@"name"];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MZSection *s = _minzuSections[indexPath.section];
//    MZNature *city = s.natures[indexPath.row];
    NSLog(@"%@",s.natures[indexPath.row]);
    
    [MZMetaDataTool sharedMZMetaDataTool].currentCity = s.natures[indexPath.row];
    
    _detailViewController = [[MZdetailViewController alloc] init];
    _detailViewController.minzu = s.natures[indexPath.row];

    
    [self.navigationController pushViewController:_detailViewController animated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
